package com.example.eduservice.controller;


import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.commonutils.Result;
import com.example.eduservice.entity.EduCourse;
import com.example.eduservice.entity.query.CourseQuery;
import com.example.eduservice.entity.vo.CourseInfoVo;
import com.example.eduservice.entity.vo.CoursePublishVo;
import com.example.eduservice.service.EduCourseService;
import com.mysql.cj.CoreSession;
import com.mysql.cj.log.Log;
import com.sun.corba.se.impl.oa.toa.TOA;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Anticipate
 * @since 2022-03-13
 */
@Api(description = "添加课程信息")
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    // 添加课程基本信息
    @PostMapping("addCourseInfo")
    public Result addCourseInfo(@RequestBody CourseInfoVo courseInfo) {
        // 返回添加之后的课程id， 为了后面添加大纲使用
        String id = courseService.saveCourseInfo(courseInfo);
        return Result.ok().data("courseId", id);
    }

    // 根据课程id查询信息
    @GetMapping("getCourseById/{courseId}")
    public Result getCourseById(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseById(courseId);
        System.out.println(courseInfoVo.getSubjectParentId());
        System.out.println(courseInfoVo.getSubjectId());
        return Result.ok().data("courseInfoVo", courseInfoVo);
    }

    // 修改课程信息
    @PostMapping("updateCourseInfo")
    public Result updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {

        courseService.updateCourseInfo(courseInfoVo);
        return Result.ok();
    }

    // 查询发布课程信息
    @GetMapping("selectCoursePublishVoById/{courseId}")
    public Result selectCoursePublishVoById(@PathVariable String courseId) {
        CoursePublishVo coursePublishVo = courseService.selectCoursePublishVoById(courseId);
        return Result.ok().data("coursePublishInfo", coursePublishVo);
    }

    // 课程分页
    @GetMapping("coursePageList/{current}/{limit}")
    public Result coursePageList(@PathVariable Long current,
                                 @PathVariable Long limit) {
        Page<EduCourse> pageParam = new Page<>(current, limit);
        courseService.page(pageParam, null);
        List<EduCourse> pageList = pageParam.getRecords();
        long total = pageParam.getTotal();
        return Result.ok().data("total", total).data("pageList", pageList);
    }

    // 课程条件查询分页
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public Result pageCourseCondition(@PathVariable Long current,
                                      @PathVariable Long limit,
                                      @RequestBody(required = false) CourseQuery courseQuery) {
        Page<EduCourse> coursePage = new Page<>(current, limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        String title = courseQuery.getTitle();
        String teacherId = courseQuery.getTeacherId();
        String subjectId = courseQuery.getSubjectId();
        String subjectParentId = courseQuery.getSubjectParentId();
        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(teacherId)) {
            wrapper.eq("teacher_id", teacherId);
        }
        if (!StringUtils.isEmpty(subjectId)) {
            wrapper.eq("subject_id", subjectId);
        }
        if (!StringUtils.isEmpty(subjectParentId)) {
            wrapper.eq("subject_parent_id", subjectParentId);
        }
        wrapper.orderByDesc("gmt_modified");
        courseService.page(coursePage, wrapper);

        List<EduCourse> pageCourseList = coursePage.getRecords();
        long total = coursePage.getTotal();
        return Result.ok().data("list", pageCourseList).data("total", total);

    }

    // 删除课程
    @DeleteMapping("deleteCourseByCourseId/{courseId}")
    public Result deleteCourseByCourseId(@PathVariable String courseId){
        courseService.deleteCourseByCourseId(courseId);
        return Result.ok();
    }
}





































